package com.aifeng.hassan_food.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.aifeng.base.BaseActivity
import com.aifeng.base.addItems
import com.aifeng.base.loadLinear
import com.aifeng.base.refresh
import com.aifeng.common_ui.base.getColorEx
import com.aifeng.common_ui.base.gone
import com.aifeng.common_ui.base.visible
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.model.CommonData
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByFinally
import com.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.textColor
import org.koin.androidx.viewmodel.ext.android.getViewModel

class WalletDetailActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "收入明细")

        swipe_refresh.isRefreshing = true
        getData(pageNum)
    }

    override fun getViewModel() = getViewModel<WalletViewModel>()

    @SuppressLint("SetTextI18n")
    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        empty_hint.text = "暂无相关明细信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.loadLinear(baseContext, swipe_refresh) {
            if (!isLoadingMore) {
                isLoadingMore = true
                getData(pageNum)
            }
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_wallet_list) { data, injector ->
                injector.text(R.id.item_wallet_title, data.remark)
                    .text(R.id.item_wallet_time, data.create_date.toNotLong().getTimeString())
                    .text(R.id.item_wallet_balance, "余额 ${data.balance.formatDecimal()}")
                    .with<TextView>(R.id.item_wallet_money) {
                        val amount = data.amount.toNotDouble()
                        if (amount < 0) {
                            it.textColor = getColorEx(R.color.black)
                            it.text = amount.formatDecimal()
                        } else {
                            it.textColor = getColorEx(R.color.colorAccent)
                            it.text = "+${amount.formatDecimal()}"
                        }
                    }
            }
            .attachTo(recycle_list)
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().balanceList(index, getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (com.aifeng.base.count(it) > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

}
